-
Notifications
You must be signed in to change notification settings - Fork 0
392. Is Subsequence #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
def isSubsequence(self, s: str, t: str) -> bool: | ||
i = 0 | ||
for c in t: | ||
if i == len(s): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i == len(s)
が重複しているのが気になりました。
for c in t:
if s[i] != c:
continue
i += 1
if i == len(s):
return True
return False
と書くと良いかもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
その方法だとsが空文字のときを別で考慮しないとIndexErrorになるんですよね、ただ見比べるとご提案いただいた方が素直な気がしました。
- 空間計算量: O(n) | ||
|
||
```py | ||
class Solution: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この解き方は思いつきませんでした。面白いと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
色々な解法を試していてとても良いと思いました。僕も参考にさせていただきます
所要時間: 0:47 | ||
|
||
m: len(s), n: len(t) | ||
- 時間計算量: 分からない。1文字あたりパターンに対して指数時間かかるとすると O(n * 2^m) か? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pythonの正規表現エンジンはバックトラッキングを使用しているようなのでその時間計算量と空間計算量で合ってる気はします(僕も確信はないですが)
def isSubsequence(self, s: str, t: str) -> bool: | ||
i = -1 | ||
for c in s: | ||
i = t.find(c, i + 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
時々標準ライブラリーを見ると発見がありますね。
char_to_indexes[c].append(i) | ||
prev_index = -1 | ||
for c in s: | ||
indexes = char_to_indexes[c] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
解き方面白いです。
indexesの生存期間が長いので、indexes_of_cくらいの説明があってもいいかもしれませんが(自分は1回目読んでる時最後の方で忘れた)、好みの範囲かもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indexesも使われれますが、indicesという複数形も使われます(元々のラテン語のようです)
matrix
-> matrices
index
-> indices
などと同じですね。
個人的には教科書などではindicesの方がよく見かける気がしました。
https://stackoverflow.com/questions/1378781/proper-terminology-should-i-say-indexes-or-indices
https://leetcode.com/problems/is-subsequence/description/